from Crypto import Random
from Crypto.Hash import SHA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
from Crypto.Signature import PKCS1_v1_5 as Signature_pkcs1_v1_5
from Crypto.PublicKey import RSA
import base64
import binascii


def rsaclass(message):
    with open("tools/private.pem") as f:
        key = f.read()
        rsakey = RSA.importKey(key)
        signer = Signature_pkcs1_v1_5.new(rsakey)
        digest = SHA.new()
        digest.update(message.encode())
        sign = signer.sign(digest)
        # signature = base64.b64encode(sign)
        # print(signature.decode())
        # return signature.decode()
        return binascii.b2a_hex(sign).decode()
        # return sign

    # with open("public.pem") as f:
    #     key = f.read()
    #     rsakey = RSA.importKey(key)
    #     verifier = Signature_pkcs1_v1_5.new(rsakey)
    #     digest = SHA.new()
    #     # Assumes the data is base64 encoded to begin with
    #     digest.update(message.encode())
    #     is_verify = signer.verify(digest, base64.b64decode(signature))
    #     print(is_verify)

if __name__ == '__main__':
    print(rsaclass('123'))